cd /d %~dp0
set path=%path%;<%launchPath%>/helper
set app=<%appstart%>
set app=%app:/=\%
set bin=<%binPath%>
set bin=%bin:/=\%

set >env.properties
cat "%app%" | md5sum  | grep -Pio "[0-9a-f]{32}" >appid
set /p appid=<appid
wmic process where (Name="wmic.exe" AND CommandLine LIKE "%%%appid%%TIME%%%") get ParentProcessId /value | grep -Pi parentprocessid= | grep  -Po \d+ >"%app%.pid"
cat "%app%.pid"

set init=%bin%\init.bat

if not exist %init% goto startApp

echo Found %bin%\init.bat
set status_file=%bin%\..\etc\status

linuxdate "+%%s" >temp.start_time
set /p start_time=<temp.start_time
del temp.start_time



:check_status
rem cat %status_file% | grep -Po "^\s*status\s*=\s*\d\s*$" | grep -Po "\d"
cat %status_file% | grep -Po "^\s*status\s*=\s*\d+" | grep -Po "\d+" >temp.status_code
set /p status=<temp.status_code
echo status:%status%
del temp.status_code

if "%status%" == "2" goto startApp


linuxdate "+%%s" >temp.now_time
set /p now_time=<temp.now_time
del temp.now_time

set /a elapse=now_time-start_time
echo elapse:%elapse%

linuxdate "+%%s" >temp.now_time
set /p now_time=<temp.now_time
del temp.now_time

set /a elapse=now_time-start_time
echo elapse:%elapse%
if "%status%" == "1" (
    if %elapse% gtr 3 goto end
    echo wait init ...
    ping -n 10 127.0.0.1 >nul
    goto check_status
)
if not defined status echo status=1 >%status_file%
echo call init begin ...
cd /d <%binPath%>
call %init%
echo call init end ...
goto check_status


:startApp
cd /d <%binPath%>
call "%app%"

:end
cd /d %~dp0
del "%app%.pid"

